package cn.spring.ly.service.impl;

import cn.spring.ly.config.SfmSolrCore;
import cn.spring.ly.entity.Goods;
import cn.spring.ly.mapper.GoodsDao;
import cn.spring.ly.page.PageInfo;
import cn.spring.ly.service.GoodsService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2018-12-19
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {

	@Autowired
	SolrClient client;

	@Cacheable(value = "goodList", key = "#page.current+ '_' +#name+ '_' +#catePid")
	public PageInfo<Goods> selectByPage(Page<Goods> page, String name, Integer catePid) {
		return baseMapper.selectByPage(page, name, catePid);
	}

	@CacheEvict(cacheNames = "goodList", allEntries = true)
	@Override
	public void clearGoods() {

	}

	@Override
	public PageInfo<Goods> selectBySolrPage(Integer pageNo, String name) throws SolrServerException, IOException {

		PageInfo<Goods> pageInfo = new PageInfo<>(pageNo, 3);
		// 搜索条件
		SolrQuery query = new SolrQuery("*:*");
		if (!StringUtils.isEmpty(name)) {
			query.set("q", "{!term f=name}"+name);
//			query.set("df", "name");
		}
		// 分页
		query.setStart((int) pageInfo.offset());// 开始位置
		query.setRows((int) pageInfo.getSize());// 查询的行数

		// 设置高亮字段 可以是多个
		query.addHighlightField("name");
		query.setHighlightSimplePre("<span style='color:red'>");
		query.setHighlightSimplePost("</span>");

		// 执行搜索
		QueryResponse resp = client.query(SfmSolrCore.BDSHOP_CORE,query);

		// 总记录
		long totalRow = resp.getResults().getNumFound();

		// 设置总记录
		pageInfo.setTotal(totalRow);

		// 高亮结果集
		Map<String, Map<String, List<String>>> highlighting = resp.getHighlighting();
		// 结果集
		SolrDocumentList results = resp.getResults();

		List<Goods> goodsList = new ArrayList<>();
		for (SolrDocument document : results) {
			Goods good = new Goods();
			
			String id = document.get(Goods.ID).toString();
			good.setId(Integer.parseInt(id));
			good.setName(document.get(Goods.NAME).toString());
			good.setCategoryId(Integer.parseInt(document.get(Goods.CATEGORY_ID).toString()));
			good.setGoodsNumber(Integer.parseInt(document.get(Goods.GOODS_NUMBER).toString()));
			good.setCategoryName(document.get(Goods.CATEGORY_NAME).toString());
			good.setCategoryParentName(document.get(Goods.CATEGORY_PARENT_NAME).toString());
			good.setRetailPrice(BigDecimal.valueOf(Double.parseDouble(document.get(Goods.RETAIL_PRICE).toString())));
			good.setMarketPrice(BigDecimal.valueOf(Double.parseDouble(document.get(Goods.MARKET_PRICE).toString())));
			
			// 获取高亮信息
			Map<String, List<String>> map = highlighting.get(id);
			if (map != null) {
				List<String> list = map.get("name");
				if (list != null && list.size() > 0) {
					//高亮
					good.setNameHighlighting(list.get(0));
				}
			}
			goodsList.add(good);
		}
		// 设置到分页数据
		pageInfo.setRecords(goodsList);
		return pageInfo;
	}

}
